#include "commonpage.h"
#include "ui_commonpage.h"
#include<QString>
#include<QPixmap>
#include"listitembox.h"

CommonPage::CommonPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    // 不要⽔平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //点击全部播放按钮后，发送信号给lxmusic
    connect(ui->playAllBtn, &QPushButton::clicked,this, [=](){
        emit CommonPage::playAll(_pageType);
    });

    //双击音乐后，发送信号给lxmusic，让其实现播放
    connect(ui->pageMusicList,&QListWidget::doubleClicked, this, [=](const QModelIndex& index){
        emit playMusicbyIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &image)
{
    //设置标题
    ui->pageTittle->setText(title);

    //设置图片
    ui->musicImageLabel->setPixmap(QPixmap(image));

    //是图片自动缩放
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::setMusicListType(PageType pageType)
{
    _pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    //删除存在的旧数据
    _musicOfPage.clear();
    ui->pageMusicList->clear();

    for(auto& music : musicList){
        switch(_pageType){
            case PageType::LIKEPAGE:
            {
                if(music.getIsLike()){
                    _musicOfPage.push_back(music.getMusicId());
                }
                break;
            }
            case PageType::RECENTPAGE:
            {
                if(music.getIsHistory()){
                    _musicOfPage.push_back(music.getMusicId());
                }
                break;
            }
            case PageType::LOCALPAGE:
            {
                _musicOfPage.push_back(music.getMusicId());
                break;
            }
            default:
            {
                qDebug() << "功能暂未支持";
            }

        }
    }
}

void CommonPage::refreshPage(MusicList &musicList)
{
    addMusicToMusicPage(musicList);

    for(auto& musicId : _musicOfPage){
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end())continue;

        //初始化ListItemBox对象
        ListItemBox * listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSingerName(it->getSingerName());
        listItemBox->setAblumName(it->getAlbumName());
        listItemBox->setIcon(it->getIsLike());

        //绑定信号与槽
        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
            emit updateLikeMusic(isLike, it->getMusicId());
        });

        //设置QListWigetItem对象的宽和高
        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(ui->pageMusicList->width(),45));

        //添加到QListWiget中
        ui->pageMusicList->setItemWidget(item,listItemBox);
    }

    // 更新完成后刷新下界⾯
    ui->pageMusicList->repaint();
}

void CommonPage::addMusicToPlayList(MusicList &musicList, QMediaPlaylist *playList)
{
    // 根据⾳乐列表中⾳乐所属的⻚⾯，将⾳乐添加到playList中
    for(auto music : musicList){
        switch(_pageType){
            case PageType::LOCALPAGE:{
                playList->addMedia(music.getMusicUrl());
                break;
            }
            case PageType::LIKEPAGE:{
                if(music.getIsLike()){
                    playList->addMedia(music.getMusicUrl());
                }
                break;
            }
            case PageType::RECENTPAGE:{
                if(music.getIsHistory()){
                    playList->addMedia(music.getMusicUrl());
                }
                break;
            }
            default:
                qDebug() << "该页面暂未实现";
                break;
        }
    }
}

QString CommonPage::getMusicIdByIndex(const int index)
{

    if(index >= _musicOfPage.size()){
        return "";
    }

    qDebug() <<"index:" << index << "size:" <<_musicOfPage.size() << _musicOfPage[index];

    return _musicOfPage[index];

}

void CommonPage::setImageLabel(const QPixmap &pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);
}

